/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.iasd.youthshelter.wizards.core.events.graduation;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.EventListenerList;
import org.openide.WizardDescriptor;
import org.openide.WizardDescriptor.FinishablePanel;
import org.openide.util.HelpCtx;
import pt.iasd.youthshelter.datamodel.GraduationSchool;
import pt.iasd.youthshelter.wizards.core.WizardKeys;

public class EditGraduationSchoolWizardPanel1 implements WizardDescriptor.Panel<WizardDescriptor>, FinishablePanel<WizardDescriptor>, PropertyChangeListener {
    private static final ResourceBundle bundle = java.util.ResourceBundle.getBundle("pt/iasd/youthshelter/wizards/core/Bundle");

    /**
     * The visual component that displays this panel. If you need to access the
     * component from this class, just use getComponent().
     */
    private GraduationSchoolVisualPanel1 component;
    
    private WizardDescriptor descriptor;
    
    private final EventListenerList listeners = new EventListenerList();
    
    private boolean isValid = false, isFinished = false;
    
    private List<GraduationSchool> currentSchools;
    
    private String oldName;
    
    private Date oldStartDate, oldEndDate;

    // Get the visual component for the panel. In this template, the component
    // is kept separate. This can be more efficient: if the wizard is created
    // but never displayed, or not all panels are displayed, it is better to
    // create only those which really need to be visible.
    @Override
    public GraduationSchoolVisualPanel1 getComponent() {
        if (component == null) {
            component = new GraduationSchoolVisualPanel1(oldName, oldStartDate, oldEndDate);
        }
        component.putClientProperty(WizardDescriptor.PROP_AUTO_WIZARD_STYLE, true);
        return component;
    }

    @Override
    public HelpCtx getHelp() {
        // Show no Help button for this panel:
        return HelpCtx.DEFAULT_HELP;
        // If you have context help:
        // return new HelpCtx("help.key.here");
    }

    @Override
    public boolean isValid() {
        return isValid;
        // If it depends on some condition (form filled out...) and
        // this condition changes (last form field filled in...) then
        // use ChangeSupport to implement add/removeChangeListener below.
        // WizardDescriptor.ERROR/WARNING/INFORMATION_MESSAGE will also be useful.
    }

    @Override
    public boolean isFinishPanel() {
        return isFinished;
    }

    @Override
    public void addChangeListener(ChangeListener l) {
        listeners.add(ChangeListener.class, l);
    }

    @Override
    public void removeChangeListener(ChangeListener l) {
        listeners.remove(ChangeListener.class, l);
    }

    @Override
    public void readSettings(WizardDescriptor wiz) {
        this.descriptor = wiz;
        this.currentSchools = (List<GraduationSchool>)wiz.getProperty(WizardKeys.PROP_LIST);
        oldName = (String)wiz.getProperty(WizardKeys.PROP_NAME);
        oldStartDate = (Date)wiz.getProperty(WizardKeys.PROP_START_DATE);
        oldEndDate = (Date)wiz.getProperty(WizardKeys.PROP_END_DATE);
        getComponent().addPropertyChangeListener(this);
    }

    @Override
    public void storeSettings(WizardDescriptor wiz) {
        wiz.putProperty(WizardKeys.PROP_NAME, component.getNameTextField().getText());
        wiz.putProperty(WizardKeys.PROP_START_DATE, component.getStartDatePicker().getDate());
        wiz.putProperty(WizardKeys.PROP_END_DATE, component.getEndDatePicker().getDate());
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        boolean oldState = isValid;
        String name = component.getNameTextField().getText();
        Date start = component.getStartDatePicker().getDate();
        Date end = component.getEndDatePicker().getDate();
        if (name.equals("")) {
            isValid = isFinished = false;
            descriptor.getNotificationLineSupport().setErrorMessage(
                    bundle.getString("Wizard.Name.IsEmpty"));
        } else if(start.after(end)) {
            isFinished = false;
            descriptor.getNotificationLineSupport().setErrorMessage(
                    bundle.getString("Wizard.Date.Reversed"));
        } else if(name.equals(oldName)) {
            isValid = true;
            if(start.equals(oldStartDate) && end.equals(oldEndDate)) {
                isFinished = false;
//                descriptor.getNotificationLineSupport().setWarningMessage(
//                        bundle.getString("Wizard.NoChanges"));
            } else {
                if(currentSchools != null) {
                    for(GraduationSchool school : currentSchools) {
                        if(school.getName().equalsIgnoreCase(name) && !school.getStartDate().equals(oldStartDate)
                                && !school.getEndDate().equals(oldEndDate)
                                && dateOverlaps(start, end, school.getStartDate(), school.getEndDate())) {
                            isValid = isFinished = false;
                            descriptor.getNotificationLineSupport().setErrorMessage(
                                    bundle.getString("GraduationSchool.Exists.Error"));
                            break;
                        }
                    }
                }
                if(isValid) {
                    descriptor.getNotificationLineSupport().clearMessages();
                }
            }
        } else {
            isValid = true;
            descriptor.getNotificationLineSupport().clearMessages();
        }
        if(oldState != isValid) {
            ChangeEvent event = new ChangeEvent(this);
            for(ChangeListener listener : listeners.getListeners(ChangeListener.class)) {
                listener.stateChanged(event);
            }
        }
    }
    
    // <editor-fold defaultstate="collapsed" desc="Date Analisys Code">
    private boolean dateOverlaps(Date newStartDate, Date newEndDate, Date oldStartDate, Date oldEndDate) {
        if(!newStartDate.before(oldEndDate)) {
            return false;
        } else if(!newEndDate.after(oldStartDate)) {
            return false;
        } else {
            return true;
        }
    }// </editor-fold>
}
